/*
 * To change this template, choose Tools | Templates
 * and open the template in the editor.
 */
package ca.trader.beans.java;

import ca.trader.beans.enterprise.SaleBeanLocal;
import ca.trader.logger.TraderLogger;
import ca.trader.sale.Sale;
import ca.trader.utils.HttpUtils;
import java.util.List;
import javax.servlet.http.HttpSession;

/**
 *
 * @author Ahsan
 */
public class UserFavouritedSalesListLookupBean
{

	 private static final String _className = UserFavouritedSalesListLookupBean.class.getName();
	 private boolean resultsFound = false;
	 private List<Sale> favouritedSales = null;
	 private int pageNumber = -1;
	 private int saleCount;

	 public void setSession(HttpSession session)
	 {
		  final String _methodName = "setSession";

		  TraderLogger.logMethodEntry(_className, _methodName);

		  if (pageNumber == -1)
		  {
			   TraderLogger.logWarning(_className, _methodName, "Page number is not set.  Exiting");

			   TraderLogger.logMethodExit(_className, _methodName);
			   return;
		  }

		  if (session == null)
		  {
			   TraderLogger.logInfo(_className, _methodName, "HttpSession is null. Doing nothing");
		  }
		  else
		  {
			   TraderLogger.logInfo(_className, _methodName, "Finding all sales for the authenticated user");

			   favouritedSales = null;
			   resultsFound = false;

			   try
			   {
				    Integer authenticatedUserId = HttpUtils.getAuthenticatedUserID(session);

				    SaleBeanLocal saleBean = BeanUtils.getSaleBean();
				    saleCount = saleBean.getFavouritedSaleCountByUserID(authenticatedUserId);
				    if (saleCount == 0)
				    {
						TraderLogger.logInfo(_className, _methodName, "No favourited sales found for the user.");
				    }
				    else
				    {
						favouritedSales = saleBean.getSalesFavouritedByUserID(authenticatedUserId, BeanConsts.SALES_PER_PAGE * (pageNumber - 1), BeanConsts.SALES_PER_PAGE);

						if (!favouritedSales.isEmpty())
						{
							 resultsFound = true;

							 TraderLogger.logInfo(_className, _methodName, "Found sales for the page.  Count: " + favouritedSales.size());
						}
				    }
			   }
			   catch (Exception ex)
			   {
				    TraderLogger.logSevere(_className, _methodName, "Unknown exception", ex);
			   }
		  }

		  TraderLogger.logMethodExit(_className, _methodName);

	 }

	 /**
	  *
	  * @return if the user was found
	  */
	 public boolean isResultsFound()
	 {
		  //  return user != null;
		  return resultsFound;
	 }

	 public void setPageNumber(String sPageNumber)
	 {
		  final String _methodName = "setPageNumber";
		  TraderLogger.logWarning(_className, _methodName, "Setting page number: " + sPageNumber);
		  try
		  {
			   pageNumber = Integer.parseInt(sPageNumber);
		  }
		  catch (NumberFormatException ex)
		  {
			   pageNumber = -1;
			   TraderLogger.logWarning(_className, "setPageNumber", "Error setting page number: " + sPageNumber);
		  }
	 }

	 public int getTotalUserSaleCount()
	 {
		  if (!resultsFound)
		  {
			   throw new IllegalStateException("No sales have been found.");
		  }
		  return saleCount;
	 }

	 public List<Sale> getSaleList()
	 {

		  if (!resultsFound)
		  {
			   throw new IllegalStateException("No sales have been found.");
		  }
		  return favouritedSales;
	 }

	 public int getPageSaleCount()
	 {
		  if (!resultsFound)
		  {
			   throw new IllegalStateException("No sales have been found.");
		  }
		  return favouritedSales.size();
	 }

	 public String getPageNumber()
	 {

		  if (!resultsFound)
		  {
			   throw new IllegalStateException("No sales have been found.");
		  }

		  return pageNumber + "";
	 }

	 public String getTotalPageNumbers()
	 {
		  if (!resultsFound)
		  {
			   throw new IllegalStateException("No sales have been found.");
		  }

		  TraderLogger.logInfo(_className, "getTotalPageNumbers", "Total page numbers: " + ((int) Math.ceil(((double) saleCount) / ((double) BeanConsts.SALES_PER_PAGE))) + "");

		  return ((int) Math.ceil(((double) saleCount) / ((double) BeanConsts.SALES_PER_PAGE))) + "";
	 }

}
